/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/10 23:26
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {

    var api = {
        getSliderSetting: '/bbs/slider/getSetting',
        setSliderSetting: '/bbs/slider/setSetting'
    };

    sm.controller('admin-bbs-slider-setting-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {


        $scope.viewThumb = '';
        $scope.setting = {
            nickname: '',
            thumb: ''
        };

        $scope.getSetting = function () {
            $http.get(api.getSliderSetting).success(function (res) {
                if (res.state && res.data) {
                    $scope.setting.nickname = res.data.nickname;
                    $scope.setting.thumb = res.data.thumb;
                    $scope.viewThumb = res.data.viewThumb;
                }
            });
        };

        $scope.getSetting();

        $scope.submit = function () {

            if (!$scope.setting.nickname) {
                return false;
            }

            $http.post(api.setSliderSetting, $scope.setting).success(function (res) {
                if (res.state) {
                    Modal.success('设置成功');
                } else {
                    Modal.error('设置失败');
                }
            });

        };


        function upload() {
            // 初始化Web Uploader
            var uploader = WebUploader.create({
                auto: true,
                server: '/bbs/publish/uploadAttachment?width=100&height=100',
                pick: '#thumbBtn',
                swf: '/assets/library/webuploader/Uploader.swf',
                duplicate: true,
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,png'
                }
            });

            // 成功
            uploader.on('uploadSuccess', function(file, res) {
                if (res.state) {
                    var data = res.data;
                    $scope.setting.thumb = data.key;
                    $scope.viewThumb = data.thumb;
                } else {
                    var error = res.error ? res.error : '上传失败';
                    Modal.error(error);
                }
            });

            uploader.on('uploadComplete', function( file ) {
                $scope.$apply();
                delete uploader;
                upload();
            });
        }
        upload();

    }]);

})();